package controllers

import (
	"encoding/json"
	"fmt"
	"io"
	"net/http"
	"path/filepath"
	"shudao-chat-go/models"
	"strings"
	"time"

	"github.com/beego/beego/v2/server/web"
)

type TotalController struct {
	web.Controller
}

// 随机返回5条推荐问题
func (c *TotalController) GetRecommendQuestion() {
	var result []map[string]interface{}

	// 直接查询需要的字段，避免循环
	models.DB.Model(&models.RecommendQuestion{}).
		Select("question").
		Order("rand()").
		Limit(5).Where("is_deleted = ?", 0).
		Find(&result)

	c.Data["json"] = map[string]interface{}{
		"statusCode": 200,
		"msg":        "success",
		"data":       result,
	}
	c.ServeJSON()
}

// 提交意见反馈
func (c *TotalController) SubmitFeedback() {
	var feedback models.FeedbackQuestion
	// 尝试解析JSON数据
	if err := json.Unmarshal(c.Ctx.Input.RequestBody, &feedback); err != nil {
		c.Data["json"] = map[string]interface{}{
			"statusCode": 400,
			"msg":        "JSON解析失败: " + err.Error(),
		}
		c.ServeJSON()
		return
	}
	// 打印解析后的数据
	fmt.Println("解析后的数据:", feedback)

	tx := models.DB.Begin()
	if err := tx.Create(&feedback).Error; err != nil {
		tx.Rollback()
		c.Data["json"] = map[string]interface{}{
			"statusCode": 500,
			"msg":        "fail",
		}
		c.ServeJSON()
		return
	}
	tx.Commit()
	c.Data["json"] = map[string]interface{}{
		"statusCode": 200,
		"msg":        "success",
	}
	c.ServeJSON()
}

// 返回政策文件
func (c *TotalController) GetPolicyFile() {
	policy_type, _ := c.GetInt("policy_type")
	search := c.GetString("search")
	page, _ := c.GetInt("page")
	pageSize, _ := c.GetInt("pageSize")
	var policyFile []models.PolicyFile
	offset := (page - 1) * pageSize
	if policy_type == 0 {
		models.DB.Model(&models.PolicyFile{}).Where("is_deleted = ? AND policy_name LIKE ?", 0, "%"+search+"%").Offset(offset).Limit(pageSize).Find(&policyFile)
	} else {
		models.DB.Model(&models.PolicyFile{}).Where("is_deleted = ? AND policy_type = ? AND policy_name LIKE ?", 0, policy_type, "%"+search+"%").Offset(offset).Limit(pageSize).Find(&policyFile)
	}

	c.Data["json"] = map[string]interface{}{
		"statusCode": 200,
		"msg":        "success",
		"data":       policyFile,
	}
	c.ServeJSON()
}

// 随机返回四条功能卡片
func (c *TotalController) GetFunctionCard() {
	//问题类型，0为AI问答，1为安全培训
	functionType, _ := c.GetInt("function_type")
	var functionCard []models.FunctionCard
	models.DB.Model(&models.FunctionCard{}).Order("rand()").Limit(4).Where("function_type = ? AND is_deleted = ?", functionType, 0).Find(&functionCard)
	c.Data["json"] = map[string]interface{}{
		"statusCode": 200,
		"msg":        "success",
		"data":       functionCard,
	}
	c.ServeJSON()
}

// 随机返回三条热点问题
func (c *TotalController) GetHotQuestion() {
	//问题类型，0为AI问答，1为安全培训
	questionType, _ := c.GetInt("question_type")
	var hotQuestion []models.HotQuestion
	models.DB.Model(&models.HotQuestion{}).Order("rand()").Limit(4).Where("question_type = ? AND is_deleted = ?", questionType, 0).Find(&hotQuestion)
	c.Data["json"] = map[string]interface{}{
		"statusCode": 200,
		"msg":        "success",
		"data":       hotQuestion,
	}
	c.ServeJSON()
}

type LikeAndDislikeRequest struct {
	ID           uint `json:"id"`
	UserFeedback int  `json:"user_feedback"`
}

// 点赞和踩
func (c *TotalController) LikeAndDislike() {
	var likeAndDislike LikeAndDislikeRequest
	if err := json.Unmarshal(c.Ctx.Input.RequestBody, &likeAndDislike); err != nil {
		c.Data["json"] = map[string]interface{}{
			"statusCode": 400,
			"msg":        "JSON解析失败: " + err.Error(),
		}
		c.ServeJSON()
		return
	}
	id := likeAndDislike.ID
	userFeedback := likeAndDislike.UserFeedback
	models.DB.Model(&models.AIMessage{}).Where("id = ?", id).Update("user_feedback", userFeedback)
	c.Data["json"] = map[string]interface{}{
		"statusCode": 200,
		"msg":        "success",
	}
	c.ServeJSON()
}


// 下载文件接口，支持从OSS链接下载文件并返回给前端
func (c *TotalController) GetPdfOssDownloadLink() {
	pdfOssDownloadLink := c.GetString("pdf_oss_download_link")
	customFileName := c.GetString("file_name") // 支持自定义文件名
	
	// 验证URL是否为空
	if pdfOssDownloadLink == "" {
		c.Data["json"] = map[string]interface{}{
			"statusCode": 400,
			"msg":        "下载链接不能为空",
		}
		c.ServeJSON()
		return
	}

	// 创建HTTP客户端，设置超时时间
	client := &http.Client{
		Timeout: 30 * time.Second,
	}

	// 发送GET请求下载文件
	resp, err := client.Get(pdfOssDownloadLink)
	if err != nil {
		c.Data["json"] = map[string]interface{}{
			"statusCode": 500,
			"msg":        "下载文件失败: " + err.Error(),
		}
		c.ServeJSON()
		return
	}
	defer resp.Body.Close()

	// 检查HTTP状态码
	if resp.StatusCode != http.StatusOK {
		c.Data["json"] = map[string]interface{}{
			"statusCode": 500,
			"msg":        "文件下载失败，状态码: " + fmt.Sprintf("%d", resp.StatusCode),
		}
		c.ServeJSON()
		return
	}

	// 从URL中提取文件名
	fileName := filepath.Base(pdfOssDownloadLink)
	if fileName == "." || fileName == "/" {
		fileName = "download_file_" + fmt.Sprintf("%d", time.Now().Unix())
	}
	
	// 如果提供了自定义文件名，使用自定义文件名
	if customFileName != "" {
		fileName = customFileName
	}

	// 从Content-Disposition头中获取文件名（如果存在）
	if contentDisposition := resp.Header.Get("Content-Disposition"); contentDisposition != "" {
		if strings.Contains(contentDisposition, "filename=") {
			parts := strings.Split(contentDisposition, "filename=")
			if len(parts) > 1 {
				fileName = strings.Trim(parts[1], "\"")
			}
		}
	}

	// 获取文件大小
	contentLength := resp.Header.Get("Content-Length")
	
	// 设置文件下载响应头
	c.Ctx.Output.Header("Content-Type", "application/octet-stream")
	c.Ctx.Output.Header("Content-Disposition", fmt.Sprintf("attachment; filename=\"%s\"", fileName))
	if contentLength != "" {
		c.Ctx.Output.Header("Content-Length", contentLength)
	}
	c.Ctx.Output.Header("Cache-Control", "no-cache")

	// 将文件内容写入响应（流式传输）
	_, err = io.Copy(c.Ctx.ResponseWriter, resp.Body)
	if err != nil {
		c.Data["json"] = map[string]interface{}{
			"statusCode": 500,
			"msg":        "文件传输失败: " + err.Error(),
		}
		c.ServeJSON()
		return
	}
	
	// 注意：文件下载成功后，响应已经通过io.Copy发送给客户端
	// 此时不能再调用c.ServeJSON()，因为响应已经完成
}
